package com.one;

import com.google.gson.Gson;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

public class Util {
    /** 传入响应对象及需要响应的数据，将数据以JSON格式响应 */
    public static void writerData(HttpServletResponse resp, List list){
        Gson gson = new Gson();
        String result=gson.toJson(list);
        resp.setContentType("application/json");
        resp.setCharacterEncoding("utf-8");
        try {
            resp.getWriter().print(result);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /** 生成订单的编号 */
    public static String createOrderId(){
        LocalDateTime now = LocalDateTime.now();
        return ""+now.getYear()+(now.getMonthValue()<10?"0"+now.getMonthValue():now.getMonthValue())
                +(now.getDayOfMonth()<10?"0"+now.getDayOfMonth():now.getDayOfMonth())
                +now.toLocalTime().toString().replace(".","").replace(":","");
    }
}
